package cn.edu.usst.cs.goldfish.booking.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Scanner;


public class Generator {

    public static void main(String[] args) {
        File file = new File(".env");
        String url = null;
        String db_name = null;
        String user = null;
        String password = null;
        if (file.exists()) {
            try (Scanner scanner = new Scanner(file)) {
                String line;
                while (scanner.hasNextLine()) {
                    line = scanner.nextLine();
                    System.out.println(line);
                    if (line.startsWith("DB_URL")) {
                        url = line.substring(line.indexOf("=") + 1);
                        db_name = url.substring(url.lastIndexOf("/") + 1);
                        user = db_name;
                    } else if (line.startsWith("DB_USER")) {
                        user = line.substring(line.indexOf("=") + 1);
                    } else if (line.startsWith("DB_PASSWORD")) {
                        password = line.substring(line.indexOf("=") + 1);
                    }
                }
                System.out.println("url: " + url);
                System.out.println("user: " + user);
                System.out.println("password: " + password);
                System.out.println("db_name: " + db_name);

            } catch (IOException e) {
                e.printStackTrace();
                Runtime.getRuntime().exit(0);
                return;
            }
        } else {
            try (Scanner scanner = new Scanner(System.in)) {
                System.out.println("请输入数据库连接信息：");
                System.out.print("请输入数据库连接地址：");
                url = scanner.nextLine();
                System.out.print("请输入数据库用户名：");
                user = scanner.nextLine();
                System.out.print("请输入数据库密码：");
                password = scanner.nextLine();
            } catch (Exception e) {
                e.printStackTrace();
                Runtime.getRuntime().exit(0);
                return;
            }
        }
        Path outputDir = Paths.get(System.getProperty("user.dir"), "generated", "main", "java");
        Path xmlOutputDir = Paths.get(System.getProperty("user.dir"), "generated", "main", "resources", "cn/edu/usst/cs/goldfish/booking/meetingroom/mybatis");
        FastAutoGenerator
                .create(url, user, password)
                .globalConfig(builder -> {

                    System.out.println("outputDir: " + outputDir);
                    builder.author("Jerry") // 作者
                            .outputDir(outputDir.toString()) // 输出路径
                            .commentDate("yyyy-MM-dd");
                })
                .packageConfig(builder -> {
                    builder.parent("cn.edu.usst.cs.goldfish.booking.meetingroom.model")

                            .pathInfo(Collections.singletonMap(OutputFile.mapper, xmlOutputDir.toString()));
                })
                .strategyConfig(builder -> {
                    builder.addInclude() // 你要生成的表，空表示全部
                            .entityBuilder().enableLombok(); // 生成实体用lombok
                })
                .execute();
    }
}
